import itertools

# 定义每种情况的参数
params = [
    {"p1": 0.10, "c1": 4, "cd1": 2, "p2": 0.10, "c2": 18, "cd2": 3, "pp": 0.10, "cp": 6, "cdp": 3, "r": 56, "cswap": 6,
     "cscrap": 5},
    {"p1": 0.20, "c1": 4, "cd1": 2, "p2": 0.20, "c2": 18, "cd2": 3, "pp": 0.20, "cp": 6, "cdp": 3, "r": 56, "cswap": 6,
     "cscrap": 5},
    {"p1": 0.10, "c1": 4, "cd1": 2, "p2": 0.10, "c2": 18, "cd2": 3, "pp": 0.10, "cp": 6, "cdp": 3, "r": 56, "cswap": 30,
     "cscrap": 5},
    {"p1": 0.20, "c1": 4, "cd1": 1, "p2": 0.20, "c2": 18, "cd2": 1, "pp": 0.20, "cp": 6, "cdp": 2, "r": 56, "cswap": 30,
     "cscrap": 5},
    {"p1": 0.10, "c1": 4, "cd1": 8, "p2": 0.20, "c2": 18, "cd2": 1, "pp": 0.10, "cp": 6, "cdp": 2, "r": 56, "cswap": 10,
     "cscrap": 5},
    {"p1": 0.05, "c1": 4, "cd1": 2, "p2": 0.05, "c2": 18, "cd2": 3, "pp": 0.05, "cp": 6, "cdp": 3, "r": 56, "cswap": 10,
     "cscrap": 40}
]


# 定义计算成本和收益的函数
def calculate_costs_and_revenue(decision, param):
    d1, d2, dp, ds = decision

    # 检测成本
    cost_d1 = d1 * param['cd1']
    cost_d2 = d2 * param['cd2']
    cost_dp = dp * param['cdp']

    # 装配成本
    cost_assembly = param['cp']

    # 不合格成品的调换损失
    swap_loss = (1 - dp) * param['pp'] * param['cswap']

    # 不合格成品的拆解费用
    scrap_cost = ds * param['cscrap']

    # 总成本
    total_cost = cost_d1 + cost_d2 + cost_dp + cost_assembly + swap_loss + scrap_cost

    # 收益
    revenue = param['r'] * (1 - param['pp'])

    # 净利润
    profit = revenue - total_cost

    return total_cost, revenue, profit


# 暴力求解：遍历所有可能的决策组合（d1, d2, dp, ds）
def find_best_decision(param):
    best_decision = None
    best_profit = float('-inf')
    best_cost = None
    best_revenue = None

    # 遍历所有可能的决策组合
    for decision in itertools.product([0, 1], repeat=4):
        total_cost, revenue, profit = calculate_costs_and_revenue(decision, param)

        # 更新最优解
        if profit > best_profit:
            best_profit = profit
            best_decision = decision
            best_cost = total_cost
            best_revenue = revenue

    return best_decision, best_cost, best_revenue, best_profit


# 对每种情况进行求解
for i, param in enumerate(params, 1):
    best_decision, best_cost, best_revenue, best_profit = find_best_decision(param)
    print(f"情况 {i}:")
    print(f"  最优决策 (d1, d2, dp, ds): {best_decision}")
    print(f"  总成本: {best_cost}")
    print(f"  收益: {best_revenue}")
    print(f"  净利润: {best_profit}")
    print()
